package com.example.lab.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.entity.*;
import com.example.lab.service.EquipmentService;
import com.example.lab.service.EquipmentVoService;
import com.example.lab.service.LabVoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (Equipment)表控制层
 *
 * @author makejava
 * @since 2022-04-27 17:58:01
 */
@RestController
@RequestMapping("equipment")
public class EquipmentController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private EquipmentService equipmentService;
    @Resource
    private EquipmentVoService equipmentVoService;
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param equipmentVo 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public MyReturn selectAll(Integer limit, Integer page, EquipmentVo equipmentVo) {
        Integer page1=(page-1)*limit;
        Integer limit1=limit;
        List<EquipmentVo> equipmentVos = equipmentVoService.queryAll(limit1, page1, equipmentVo);
        /*Page<Equipment> equipmentPage= new Page<>(page,limit);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(equipment.getEname())){
            queryWrapper.like("Ename",equipment.getEname());
        }
        if(ObjectUtil.isNotEmpty(equipment.getElid())){
            queryWrapper.like("ELid",equipment.getElid());
        }
        Page<Equipment> page1=this.equipmentService.page(equipmentPage,queryWrapper);*/
        Long count = equipmentVoService.getCount();
        return super.success(equipmentVos,count);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.equipmentService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param equipment 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public  MyReturn insert(@RequestBody Equipment equipment) {
        equipment.setEtime(new Date());
        boolean save= this.equipmentService.save(equipment);
        if(save){
            return  super.success(equipment,1L);
        }else {
            return super.failed(equipment,1L);
        }

    }
    /**
     * 修改数据
     *
     * @param equipment 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public  MyReturn update(@RequestBody Equipment equipment) {
        boolean update=this.equipmentService.updateById(equipment);
        if(update){
            return  super.success(equipment,1L);
        }else {
            return  super.failed(equipment,1L);
        }
    }
    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public  MyReturn delete(@RequestParam("idList[]") List<Long> idList) {
        boolean b=equipmentService.removeByIds(idList);
        if(b){
            return super.success(idList);
        }else {
            return super.failed(idList);
        }
    }
    @RequestMapping("/getAllEquipmentNoPage")
    public MyReturn getAllEquipmentNoPage(){
        List<Equipment> list = equipmentService.list();
        return super.success(list, (long) list.size());
    }
}

